﻿(function ($) {
    function _validate(formId, urles, messages) {
        $.my_validate("#" + formId, urles, messages);
        $('#' + formId).submit(function () {
            
            if ($.check_validate("#" + formId)) {
                $(this).ajaxSubmit(function (data) {
                    var jsonData = $.parseJSON(data);
                    Success(jsonData);
                });
            }
            return false;
        });
    }
    $.extend($, {
        validate_message: function (formId) {
            var urles = {
                name: {
                    required: true,

                },
                tel: {
                    required: true,
                    regex: /^1\d{10}$|^(0\d{2,3}-?|\(0\d{2,3}\))?[1-9]\d{4,7}(-\d{1,8})?$/
                },
                content: {
                    required: true,
                },
            };
            var messages = {
                name: {
                    required: '姓名必填',
                },
                tel: {
                    required: '电话必填',
                    regex: '电话格式不正确',
                },
                content: {
                    required: '留言内容必填',
                    
                },
            };
            _validate(formId, urles, messages)
        },
        validate_register: function (formId, mt) {
            var urles = {
                account: {
                    required: true,
                    regex: "^\\w+$",

                    rangelength: [5, 16],
                    remote: {
                        url: '/customer/check_unique?mark=' + mt,
                        type: 'get',
                        dataType: 'json',
                    }
                },
                pass: {
                    required: true,
                    rangelength: [5, 16],
                },
                email: {
                    required: true,
                    regex_email: /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/,
                },
                passconf: {
                    required: true,
                    rangelength: [5, 16],
                    equalTo: '#pass'
                },
              
                phone: {
                    required: true,
                    regex_phone: /^(0|86|17951)?(13[0-9]|15[012356789]|17[01678]|18[0-9]|14[57])[0-9]{8}$/,
                }
            };
            var messages = {
                account: {
                    required: '用户名必填',
                    regex: "用户名只能以数字",
                    rangelength: '用户名长度为5-16个字符',
                    remote: '用户名已存在',
                },
                pass: {
                    required: '密码必填',
                    rangelength: '密码长度为5-16个字符',
                },
                email: {
                    required: "邮箱必须填写",
                    regex_email: "请填写正确的邮箱",
                },
                passconf: {
                    required: '密码确认必填',
                    rangelength: '密码确认长度为5-16个字符',
                    equalTo: '密码不一致'
                },
                phone: {
                    required: "请填写手机",
                    regex_tel: "手机格式错误",
                }
            };
            _validate(formId, urles, messages)
        },
        validate_update_d: function (formId, mt,cid) {
            var urles = {
                account: {
                    required: true,
                    regex: "^\\w+$",

                    rangelength: [5, 16],
                    remote: {
                        url: '/customer/check_unique?mark=' + mt + '&cid=' + cid,
                        type: 'get',
                        dataType: 'json',
                    }
                },
               
                email: {
                    required: true,
                    regex_email: /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/,
                },
                phone: {
                    required: true,
                    regex_phone: /^(0|86|17951)?(13[0-9]|15[012356789]|17[01678]|18[0-9]|14[57])[0-9]{8}$/,
                }
            };
            var messages = {
                account: {
                    required: '用户名必填',
                    regex: "用户名只能以数字",
                    rangelength: '用户名长度为5-16个字符',
                    remote: '用户名已存在',
                },
              
                email: {
                    required: "邮箱必须填写",
                    regex_email: "请填写正确的邮箱",
                },
                phone: {
                    required: "请填写手机",
                    regex_tel: "手机格式错误",
                }
            };
            _validate(formId, urles, messages)
        },
        customer_add: function (formId) {
            var urles = {
                c_name: {
                    required: true,
                    rangelength: [2, 16],
                },
                detailed_add: {
                    required: true,

                },
                phone: {
                    required: true,
                    regex_phone: /^(0|86|17951)?(13[0-9]|15[012356789]|17[01678]|18[0-9]|14[57])[0-9]{8}$/,
                },
                tel: {
                    //regex_tel: /^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$/,
                },
            };
            var messages = {
                c_name: {
                    required: '姓名必填',
                    rangelength: '姓名长度为2-16个字符',
                },
                detailed_add: {
                    required: '请填写详细地址'
                },
                phone: {
                    required: "请填写手机",
                    regex_tel: "手机格式错误",
                },
                tel: {
                    //regex_tel: "电话格式错误",
                },
            };
            _validate(formId, urles, messages)
        },
        validate_dologin: function (formId) {
            var urles = {
            };
            var messages = {
            };
            _validate(formId, urles, messages)
        },
    });
}(jQuery))